package edu.jvn.ict.locationsharing;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
import edu.jvn.ict.locationsharing.informationexchange.GoingOfflineMessage;
import edu.jvn.ict.locationsharing.informationexchange.GoingOnlineMessage;
import edu.jvn.ict.locationsharing.informationexchange.JoiningGroupMessage;
import edu.jvn.ict.locationsharing.informationexchange.LeavingGroupMessage;
import edu.jvn.ict.locationsharing.informationexchange.RequestDictionaryMessage;
import edu.jvn.ict.locationsharing.informationexchange.SendBackDictinonaryMessage;
import edu.jvn.ict.locationsharing.informationexchange.UpdateLocationMessage;

public class SmsReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		// ---get the SMS message passed in---
		Bundle bundle = intent.getExtras();
		SmsMessage[] msgs = null;
		if (bundle != null) {
			// ---retrieve the SMS message received---
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];

			// ---create list of JoiningGroupMessages and exchange each of
			// them---
			ArrayList<JoiningGroupMessage> joiningGroupMessagesList = new ArrayList<JoiningGroupMessage>();
			for (int i = 0; i < msgs.length; i++) {
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
				String messageBody = msgs[i].getMessageBody();
				if (Utility.validateReceiveMessage(msgs[i].getMessageBody())) {
					switch (Utility.getMessageType(messageBody)) {
					case GoingOfflineMessage.MESSAGE_ID:
					case GoingOnlineMessage.MESSAGE_ID:
					case LeavingGroupMessage.MESSAGE_ID:
					case RequestDictionaryMessage.MESSAGE_ID:
					case UpdateLocationMessage.MESSAGE_ID:
					case SendBackDictinonaryMessage.MESSAGE_ID:
						break;
					case JoiningGroupMessage.MESSAGE_ID:
						JoiningGroupMessage joiningGroupReceiveMessage = new JoiningGroupMessage(
								Utility.getMessageContent(messageBody));
						String phoneNumber = msgs[i].getOriginatingAddress();
						if (phoneNumber.startsWith("+")) {
							phoneNumber = phoneNumber.substring(3);
						}
						if (phoneNumber.startsWith("1")) {
							phoneNumber = phoneNumber.substring(7);
						}
						joiningGroupReceiveMessage.setPhoneNumber(Long
								.valueOf(phoneNumber));
						if (joiningGroupReceiveMessage != null) {
							joiningGroupMessagesList
									.add(joiningGroupReceiveMessage);
							MemberServer.exchangeInformation(
									joiningGroupReceiveMessage, 1, true);
						}

						// update members dictionary
						for (JoiningGroupMessage joiningGroupMessage : joiningGroupMessagesList) {
							MemberInfo memberInfo = MemberServer
									.createMemberFromJGM(joiningGroupMessage);
							MemberServer.addToDictionary(memberInfo);
						}

						// Send back the current members dictionary to the new
						// members
						for (JoiningGroupMessage joiningGroupMessage : joiningGroupMessagesList) {
							MemberServer.sendDictionary(joiningGroupMessage
									.getIp(), joiningGroupMessage.getPort());
						}
						Toast.makeText(context,
								"" + MemberServer.dictionary.size(),
								Toast.LENGTH_SHORT).show();
						// Toast.makeText(context, "Message sent.",
						// Toast.LENGTH_SHORT).show();

						break;
					}
				}

			}

		}
	}

//	private JoiningGroupMessage convertFrom(SmsMessage smsMessage) {
//		String[] splits = smsMessage.getMessageBody().split(",");
//
//		if (splits.length == 3
//				&& splits[0].equalsIgnoreCase(Utility.JOINING_GROUP_REQUEST)) {
//			JoiningGroupMessage joiningGroupMessage = new JoiningGroupMessage();
//			joiningGroupMessage.setPhoneNumber(Long.valueOf(smsMessage
//					.getOriginatingAddress()));
//			joiningGroupMessage.setIp(Utility.ipToInt(splits[1]));
//			joiningGroupMessage.setJoiningTimestamp(Long.valueOf(splits[2]));
//
//			return joiningGroupMessage;
//		}
//
//		return null;
//	}
}
